package com.wise.aip.service.impl;

import java.util.Map;

import com.wise.aip.kit.WiseDataKit;
import com.wise.aip.service.BaseDataService;
import com.wise.aip.template.URLTemplate;
import com.wise.aip.token.TokenKit;

/**
 * 
 * Title: AccountDaoImpl
 * <p>
 * Description: 科目业务层
 * 
 * @author yacong_liu Email:2682505646@qq.com
 * @date 2019年4月13日
 */
public class AccountServiceImpl extends BaseDataService {
    /**
     * 
     * <p>
     * Title: save
     * </p>
     * <p>
     * Description: 新增科目
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Account
     * @param params 参数 key: name number groupID(科目类别代码)
     * @return
     * @throws Exception
     */
    public String saveAccount(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        // 默认类别为表外科目
        Integer groupID = 601;
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
            // 科目类别代码 eg:601
            groupID = Integer.valueOf("groupID");
        }

        log.append("**************" + this.getClass().getName() + "_saveAccount （新增科目）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String requestURL = URLTemplate.save(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String param = "{\"Data\":{\"FName\":'" + name + "',\"FNumber\":'" + number + "',\"FGroupID\": "
                + groupID + "}}";
        String saveRes = super.save(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + saveRes);
        log.append("\n**************************************新增科目END******************");

        System.out.println(log.toString());
        return saveRes;

    }

    /**
     * 
     * <p>
     * Title: updateAccount
     * </p>
     * <p>
     * Description: 修改科目
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Account
     * @param params 参数 key: name number oldNumber
     * @return
     * @throws Exception
     */
    public String updateAccount(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        String oldNumber = "";
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
            oldNumber = params.get("oldNumber");
        }

        log.append("**************" + this.getClass().getName() + "_updateAccount （修改科目）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String token = TokenKit.getAccessToken(gatewayHost, authorityCode);
        // 科目类别代码
        Integer groupID = WiseDataKit.getAccountGroupIDByNumber(gatewayHost, itemClassCode, token, oldNumber);
        String requestURL = URLTemplate.update(gatewayHost, itemClassCode, token);
        String param = "{\"Data\":{\"FNumber\":'" + oldNumber + "',\"Data\":{\"FName\":'" + name
                + "',\"FNumber\":'" + number + "',\"FGroupID\": " + groupID + " }}}";
        String updateRes = super.update(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + updateRes);
        log.append("\n**************************************修改科目END******************");

        System.out.println(log.toString());
        return updateRes;

    }

    /**
     * 
     * <p>
     * Title: deleteAccount
     * </p>
     * <p>
     * Description: 删除科目
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Account
     * @param number 需删除的编码
     * @return
     * @throws Exception
     */
    public String deleteAccount(String gatewayHost, String authorityCode, String itemClassCode, String number)
            throws Exception {

        StringBuffer log = new StringBuffer();
        log.append("**************" + this.getClass().getName() + "_deleteAccount （删除科目）START******\n");
        log.append("**************入参编码：" + number);

        String requestURL = URLTemplate.delete(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String deleteRes = super.deleteOrforbid(requestURL, number);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + number);
        log.append("\n*************响应结果：" + deleteRes);
        log.append("\n**************************************删除科目END******************");

        System.out.println(log.toString());
        return deleteRes;
    }

}
